package com.yy.school.util;

import com.yy.school.pojo.Leave;
import com.yy.school.pojo.Teacher;
import com.yy.school.vo.ScoreVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TeacherDBUtil extends DBUtil {
    Scanner input = new Scanner(System.in);
//查看教师个人信息
    public Teacher getTeacherByTid(Integer tid){
        Teacher tea=null;
        try {
            getConn();
            String sql="select tid,tname,sex,tel,type from `teacher` where tid=?";
            ps=conn.prepareStatement(sql);
            ps.setObject(1,tid);
            rs=ps.executeQuery();
            if(rs.next()){
                tea=new Teacher(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5)
                );
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            closeConn();
        }
        return tea;
    }
    //审核学生请假
    public List<Leave> getLeaves() {
        List<Leave> list = new ArrayList<>();
        System.out.println("请输入您的tid");
        Integer tid = input.nextInt();
        try {
            getConn();
            String sql = "select * from `leave` where tid=?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, tid);
            rs = ps.executeQuery();
            while (rs.next()) {
                Leave leave = new Leave(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getInt(4),
                        rs.getInt(5),
                        rs.getString(6),
                        rs.getDate(7),
                        rs.getDate(8)
                );
                list.add(leave);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConn();
        }
        return list;
    }
//审批学生假条
    public void updateLeaves(String choice) {
        String[] choices = choice.split(",");
        try {
            getConn();
            for (String s : choices) {
                String sql="update `leave` set status='同意' where lid=?";
                ps=conn.prepareStatement(sql);
                ps.setObject(1,s );
                int i =ps.executeUpdate();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeConn();
        }
    }
}
